﻿using System;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using DeZhen.Core.Framework.Http;

namespace DeZhen.Core.Framework.Filter
{
    public class ExceptionFilter : IAsyncExceptionFilter
    {
        public Task OnExceptionAsync(ExceptionContext context)
        {
            Exception exception = context.Exception;
            HttpResult result = HttpResult.Error(exception.Message);
            context.Result = new ContentResult
            {
                StatusCode = (int)HttpStatusCode.InternalServerError,
                ContentType = "application/json;charset=utf-8",
                Content = JsonConvert.SerializeObject(result)
            };
            context.ExceptionHandled = true;
            return Task.CompletedTask;
        }
    }
}
